<!--  -->
<template>
    <el-container class="home_container">
        <!-- 页面头部区 -->
        <el-header>
            <div>
                <img src="../../assets/logo.png" alt="logo">
                <span>电商后台管理系统</span>
            </div>
            <!-- 退出按钮 -->
            <el-button type="info" @click="handleQuitClick()">退出</el-button>
        </el-header>
        <!-- 页面中央容器区 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapase ? '64px' : '200px'">
                <!-- 侧边栏折叠按钮 -->
                <div class="tiggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区 -->
                <el-menu
                    :default-active="navActive"
                    class="el-menu-vertical-demo"
                    background-color="#333744"
                    text-color="#fff"
                    active-text-color="#409EFF"
                    :collapse="isCollapase"
                    :collapse-transition="false"
                    router
                    unique-opened
                    >
                    <!-- 1级菜单 -->
                    <!-- 注意动态绑定的index在el-menu组件里必须是字符串(内部开启了props验证)，否则就会报错！这里拼接一个空字符串做隐式转换！ -->
                    <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                        <template slot="title">
                            <!-- 1级菜单图标 -->
                            <i :class="iconObj[item.id]"></i>
                            <!-- 1级菜单文本 -->
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 2级菜单 -->
                        <!-- 注意动态绑定的index在el-menu组件里必须是字符串(内部开启了props验证)，否则就会报错！这里拼接一个空字符串做隐式转换！ -->
                        <el-menu-item :index="'/' + items.path" v-for="items in item.children" :key="items.id" @click="saveNavState('/' + items.path)">
                            <!-- 2级从菜单里的内容 -->
                            <template slot="title">
                                <!-- 2级菜单图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 2级菜单文本 -->
                                <span>{{items.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右边主题内容区 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
        menuList: [],//左侧菜单数据
        //iconfont对应渲染对象
        iconObj:{
            '125': 'iconfont icon-yonghuguanli',
            '103': 'iconfont icon-quanxianguanli',
            '101': 'iconfont icon-shangpinguanli',
            '102': 'iconfont icon-dingdanguanli',
            '145': 'iconfont icon-shujutongji'
        },
        //控制菜单栏折叠的布尔值
        isCollapase: false,
        //保存点击高亮显示的值
        navActive:''
    };
  },
  methods: {
      //退出核心思想，清空token，然后编程式路由导航跳转回登录页
      handleQuitClick() {
          window.sessionStorage.clear()
          this.$router.push('/login')
      },
      //获取菜单列表数据
      async getMenuList() {
        //解构赋值获取请求中的列表数据
        const { data:res } = await this.$http.get('menus')
        if(res.meta.status !== 200) return this.$message.error(res.meta.msg)//获取失败是弹窗错误消息告知用户
        //获取成功后保存到data中
        this.menuList = res.data
      },
      //点击按钮切换菜单的折叠与展开
      toggleCollapse() {
          this.isCollapase = !this.isCollapase
      },
      //点击显示高亮函数
      saveNavState(navActive) {
          this.navActive = navActive
          window.sessionStorage.setItem('activeNav', navActive)
      }
  },
  created() {
      //在组件加载期调用该函数，获取菜单列表数据
      this.getMenuList()
      //在组建加载期间，取出保存在sessionStorage里的高亮值
      this.navActive = window.sessionStorage.getItem('activeNav')
  }

}

</script>
<style lang='less' scoped>
// 如果自己还在上面加了一层div则该div也要这样设置，我没加！
  .home_container{
    width: 100%;
    height: 100%;
  }

  .el-header {
      widows: 100%;
      background-color: #373D41;
      display: flex;
      justify-content: space-between;
      align-items: center;
      color:#fff;
      font-size: 20px;
      >div {
          display: flex;
          align-items: center;
        img{
          width: 45px;
          height: 45px;
        }
        span{
            margin-left: 15px;
        }
      }
  }
  
  .el-aside {
      background-color: #333744;
      //去除多出来的边框线
      .el-menu {
        border-right: none;
      }
  }
  
  .el-main {
      background-color: #eaedf1;
  }

  //一级菜单图标和文字的间距
  .iconfont {
    margin-right: 10px;
  }

  //控制折叠div样式
  .tiggle-button{
      background-color: #4A5064;
      font-size: 10px;
      line-height: 24px;
      color:#fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
  }
</style>